namespace Example.Controllers
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using Example.Models;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class ContactControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<ContactController>()
                .DerivesFrom<ResourceController>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS, POST")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Resource("contact")
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new ContactController());
        }

        [Fact]
        public void op_HtmlRepresentation_CultureInfo()
        {
            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/contact.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsAssignableFrom<ViewResult>(new ContactController().HtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation_CultureInfo()
        {
            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/contact.en.xhtml").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsAssignableFrom<ViewResult>(new ContactController().XhtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Post_ContactForm()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/contact.en.xhtml").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsAssignableFrom<SeeOtherResult>(new ContactController().Post(new ContactForm()));

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Post_ContactFormNull()
        {
            Assert.Throws<ArgumentNullException>(() => new ContactController().Post(null as ContactForm));
        }
    }
}